package com.example.supermarket.service;

import com.example.supermarket.entity.Goods;
import com.example.supermarket.entity.Order;
import com.example.supermarket.entity.OrderAndGoods;
import com.example.supermarket.mapper.GoodsMapper;
import com.example.supermarket.mapper.OrderMapper;
import com.example.supermarket.utils.GiteeImgBedUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>会员查看消费记录</p>
 * @author Bosen
 * @date 2021/10/17 0:40
 */
@Service
public class MemberOrderService {

    @Autowired
    OrderMapper orderMapper;

    @Autowired
    GoodsMapper goodsMapper;

    public List<OrderAndGoods> getOrderAndGoodsList(int memberId) {
        List<OrderAndGoods> orderAndGoodsList = new ArrayList<>();
        // 获取消费记录
        List<Order> orderList = orderMapper.findByMemberId(memberId);
        for (Order order : orderList) {
            // 获取订单对应的商品信息
            Goods goods = goodsMapper.findById(order.getGoodsId());
            // 开始合并
            OrderAndGoods orderAndGoods = new OrderAndGoods();
            orderAndGoods.setId(order.getId());
            orderAndGoods.setPayStatus(order.getPayStatus());
            orderAndGoods.setPayedTime(order.getPayedTime());
            orderAndGoods.setCreateTime(order.getCreateTime());
            orderAndGoods.setOrderId(order.getOrderId());
            if (goods != null) {
                orderAndGoods.setGoodsName(goods.getName());
                orderAndGoods.setGoodsImage(GiteeImgBedUtils.PRE + goods.getImage());
                orderAndGoods.setGoodsPrice(goods.getPrice());
            }
            // 存入
            orderAndGoodsList.add(orderAndGoods);
        }
        return orderAndGoodsList;
    }
}
